import { VuexModule, Module, Action, getModule } from 'vuex-module-decorators'
import {
    getDevices, insertDevice, getDeviceColors, getDeviceLayouts, getDeviceBasics,
    getDeviceTypes, getDeviceOthersAll, updateDevice, deleteDevice,
    updateDeviceType, deleteDeviceTypes, updateDeviceLayout, deleteDeviceLayouts,
    updateDeviceBasics, deleteDeviceBasics, updateDeviceColors, deleteDeviceColors,
    getDeviceAttrs, deleteDeviceAttrs, updateDeviceAttrs, testService, refresh, screenshots,
    reboot, shutdown, udpateApk, setAutoOpen
} from '@/api/devices'
// import { getToken, setToken, removeToken } from '@/utils/cookies'
// import router, { resetRouter } from '@/router'
// import { PermissionModule } from './permission'
// import { TagsViewModule } from './tags-view'
import store from '@/store'
import { IDevice } from '@/api/types'
// import componentsRouter from '@/router/modules/components'

export interface IDeviceState {

}

@Module({ dynamic: true, store, name: 'device' })
class Devices extends VuexModule implements IDeviceState {

    @Action
    public async getDevices() {
        return new Promise((r: any, j: any) => {
            getDevices({}).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async insertDevice(obj: any) {
        return new Promise((r: any, j: any) => {
            insertDevice(obj).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }


    @Action
    public async updateDevice(device: IDevice) {
        return new Promise((r: any, j: any) => {
            updateDevice(device).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async deleteDevice(deviceId: string) {
        return new Promise((r: any, j: any) => {
            deleteDevice({ deviceId: deviceId }).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }



    @Action
    public async getDeviceColors() {
        return new Promise((r: any, j: any) => {
            getDeviceColors({}).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async getDeviceLayouts() {
        return new Promise((r: any, j: any) => {
            getDeviceLayouts({}).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async getDeviceBasics() {
        return new Promise((r: any, j: any) => {
            getDeviceBasics({}).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async getDeviceTypes() {
        return new Promise((r: any, j: any) => {
            getDeviceTypes({}).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async getDeviceOthersAll() {
        return new Promise((r: any, j: any) => {
            getDeviceOthersAll({}).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async updateDeviceType(obj: any) {
        return new Promise((r: any, j: any) => {
            updateDeviceType(obj).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async deleteDeviceTypes(typeId: any) {
        return new Promise((r: any, j: any) => {
            deleteDeviceTypes({ typeId: typeId }).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async updateDeviceLayout(obj: any) {
        return new Promise((r: any, j: any) => {
            updateDeviceLayout(obj).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async deleteDeviceLayouts(layoutId: any) {
        return new Promise((r: any, j: any) => {
            deleteDeviceLayouts({ layoutId: layoutId }).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async updateDeviceBasics(obj: any) {
        return new Promise((r: any, j: any) => {
            updateDeviceBasics(obj).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async deleteDeviceBasics(basicId: any) {
        return new Promise((r: any, j: any) => {
            deleteDeviceBasics({ basicId: basicId }).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }


    @Action
    public async updateDeviceColors(obj: any) {
        return new Promise((r: any, j: any) => {
            updateDeviceColors(obj).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async deleteDeviceColors(colorId: any) {
        return new Promise((r: any, j: any) => {
            deleteDeviceColors({ colorId: colorId }).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }


    @Action
    public async getDeviceAttrs(attrId?: any) {
        return new Promise((r: any, j: any) => {
            let data = attrId ? { attrId: attrId } : {};
            getDeviceAttrs(data).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async deleteDeviceAttrs(attrId: any) {
        return new Promise((r: any, j: any) => {
            deleteDeviceAttrs({ attrId: attrId }).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }


    @Action
    public async updateDeviceAttrs(data: any) {
        return new Promise((r: any, j: any) => {
            updateDeviceAttrs(data).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }


    @Action
    public async refreshDevice(deviceId: string) {
        return new Promise((r: any, j: any) => {
            refresh({ deviceId: deviceId }).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }


    @Action
    public async screenshots(data?: any) {
        return new Promise((r: any, j: any) => {
            screenshots({ deviceId: data }).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    /**
     * 重启
     * @param data :-1 全部在线设备
     * @returns 
     */
    @Action
    public async reboot(data?: any) {
        return new Promise((r: any, j: any) => {
            reboot({ deviceId: data }).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    /**
     * 关机
     * @param data  -1:全部在线设备
     * @returns 
     */
    @Action
    public async shutdown(data?: any) {
        return new Promise((r: any, j: any) => {
            shutdown({ deviceId: data }).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    /**
     * 更新apk
     * @param data  {deviceId:-1:全部在线设备 ,downUrl:"下载地址"}
     * @returns 
     */
    @Action
    public async udpateApk(data?: any) {
        return new Promise((r: any, j: any) => {
            udpateApk(data).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }


    @Action
    public async testService(data: any) {
        return new Promise((r: any, j: any) => {
            testService(data).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async setAutoOpen(data: { id: string, auto: string }) {
        console.error(data);
        return new Promise((r: any, j: any) => {
            setAutoOpen({ deviceId: data.id, autoOpen: data.auto }).then((value: any) => {
                console.error("Device:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

}

export const DevicesModule = getModule(Devices)
